package com.wwp.tbshop.presenter;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.wwp.tbshop.activity.orderlist.OrderListActivity;
import com.wwp.tbshop.model.bean.OrderListBaseBean;
import com.wwp.tbshop.model.bean.OrderListItemDetailBean;
import com.wwp.tbshop.model.bean.OrderListItemsBean;
import com.wwp.tbshop.model.bean.OrderSubmitResponseBean;
import com.wwp.tbshop.util.Constant;
import com.wwp.tbshop.util.SPUtils;

import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by user on 2017/6/16.
 */

public abstract class OrderListNetOperation {
    private static final String TAG = "OrderListNetOperation";
    protected Retrofit mRetrofit;
    protected OrderListService mService;
    protected Context mContext;
    private static int mUserId ;
    private OrderListActivity mOrderListActivity;

    public OrderListNetOperation(Context context) {
        mRetrofit = new Retrofit.Builder().baseUrl(Constant.HOST).addConverterFactory(GsonConverterFactory.create()).build();
        mService = mRetrofit.create(OrderListService.class);
        mContext = context;
        mUserId = Integer.parseInt(SPUtils.getString(mContext,SPUtils.USERID,"0"));
        mOrderListActivity = new OrderListActivity();
    }

    public void getOrderList(int userId, final int type, int page, int pageNum){
       userId = mUserId;
        Log.e(TAG, "getOrderList: " );
        Call<OrderListItemsBean> call = mService.getOrderList(userId,type,page,pageNum);
        call.enqueue(new Callback<OrderListItemsBean>() {
            @Override
            public void onResponse(Call<OrderListItemsBean> call, Response<OrderListItemsBean> response) {
                if(response.isSuccessful()){
                    OrderListItemsBean orderListItemsBean = response.body();
                    processListData(orderListItemsBean,type);
                }else{
                    processFailedCode(response.code());
                }
            }

            @Override
            public void onFailure(Call<OrderListItemsBean> call, Throwable t) {
                Log.e(TAG, "onFailure: " );
            }
        });
    }



    public void processListData(OrderListItemsBean orderListItemsBean, int type){
        mOrderListActivity.setData(orderListItemsBean,type);
    }


    public void getOrderDetail(int userId, int orderId){
        userId = mUserId;
        Call<OrderListItemDetailBean> call = mService.getOrderDetail(userId,orderId);
        call.enqueue(new Callback<OrderListItemDetailBean>() {
            @Override
            public void onResponse(Call<OrderListItemDetailBean> call, Response<OrderListItemDetailBean> response) {
                if(response.isSuccessful()){
                    OrderListItemDetailBean orderListItemDetailBean = response.body();
                    processDetailData(orderListItemDetailBean);
                }else{
                    processFailedCode(response.code());
                }
            }

            @Override
            public void onFailure(Call<OrderListItemDetailBean> call, Throwable t) {
                Log.e(TAG, "onFailure: " );
            }
        });
    }
    
    public void processDetailData(OrderListItemDetailBean orderListItemDetailBean){}


    public void requestCancelOrder(int userId, int orderId){
        userId = mUserId;
        Log.e(TAG, "requestCancelOrder: " );
        Call<OrderListBaseBean> call = mService.requestCancelOrder(userId,orderId);
        call.enqueue(new Callback<OrderListBaseBean>() {
            @Override
            public void onResponse(Call<OrderListBaseBean> call, Response<OrderListBaseBean> response) {

            }

            @Override
            public void onFailure(Call<OrderListBaseBean> call, Throwable t) {

            }
        });
    }

    public void orderSubmit(int userId, HashMap<String,String> orderMap){
        userId = mUserId;
        Log.e(TAG, "orderSubmit: " );
       /*
        @Header("userid")
        @Field("sku") String sku,
        @Field("addressId") String addressId,
        @Field("paymentType") String paymentType,
        @Field("deliveryType") String deliveryType,
        @Field("invoiceType") String invoiceType,
        @Field("invoiceTitle") String invoiceTitle,
        @Field("invoiceContent") String invoiceContent);*/

        Call<OrderSubmitResponseBean> call = mService.orderSubmit(userId,orderMap);
        call.enqueue(new Callback<OrderSubmitResponseBean>() {
            @Override
            public void onResponse(Call<OrderSubmitResponseBean> call, Response<OrderSubmitResponseBean> response) {
                if(response.isSuccessful()){
                    Log.e(TAG, "onResponse: 111" );
                    OrderSubmitResponseBean orderSubmitResponseBean = response.body();
                    processOrderSubmitResponse(orderSubmitResponseBean);
                }else{
                    Log.e(TAG, "onResponse: 222" );
                    processFailedCode(response.code());
                }
            }

            @Override
            public void onFailure(Call<OrderSubmitResponseBean> call, Throwable t) {
                Log.e(TAG, "onFailure: " );
            }
        });

    }

    public void processOrderSubmitResponse(OrderSubmitResponseBean orderSubmitResponseBean){};

    public void processFailedCode(int code) {
        Toast.makeText(mContext, "服务器升级中....", Toast.LENGTH_SHORT).show();
    }

}
